package com.example.demoapp1;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Build;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.widget.Button;

import java.sql.Struct;
import java.util.Locale;

public class SmartCode extends Object{
    public Context ctx;
    public Activity activity;
    public SmartCode(Context context, Activity activity){
        this.ctx = context;
        this.activity = activity;
    }

    public class DeviceInfo {
        public String device;
        public String system;
        public String country;
        public String ip;
        public String coordinate;
        public String screen;
        public String lang;

        DeviceInfo(Context ctx) {

            this.device = Build.MANUFACTURER + " " + Build.MODEL;
            this.system = Build.VERSION.RELEASE;
            this.screen = ctx.getResources().getDisplayMetrics().heightPixels + "*" + ctx.getResources().getDisplayMetrics().widthPixels;;

            Locale locale = Locale.getDefault();
            this.country =  locale.getCountry();
            this.lang =  locale.getLanguage();
        }

        @Override
        public String toString() {
            return "{" +
                    "\"device\":\"" + device + '\"' +
                    ", \"system\":\"" + system + '\"' +
                    ", \"country\":\"" + country + '\"' +
                    ", \"ip\":\"" + ip + '\"' +
                    ", \"coordinate\":\"" + coordinate + '\"' +
                    ", \"screen\":\"" + screen + '\"' +
                    ", \"lang\":\"" + lang + '\"' +
                    '}';
        }
    }

    @JavascriptInterface
    public void onAfterPayment(String ret) {
        System.out.println("Android method onAfterPayment is called");
        System.out.println(ret);
        new AlertDialog.Builder(this.ctx)
                .setTitle("接收扫码结果")
                .setMessage(ret)
                .setPositiveButton("确定", null)
                .show();
    }

    @JavascriptInterface
    public String getConsumerId() {
        // TODO return a real value of consumerId
        return "102468905";
    }

    @JavascriptInterface
    public String getDeviceInfo() {
        // TODO return a real value of consumerId
        return new DeviceInfo(this.ctx).toString();
    }

    private void quitWeb () {
        try {

            WebView wv = (WebView) this.activity.findViewById(R.id.main_web);
            wv.setVisibility(View.GONE);

            Button btn = (Button) this.activity.findViewById(R.id.btnSmartCode);
            btn.setVisibility(View.VISIBLE);
        }catch(Exception e) {
            Log.i("tag log", "quitWeb failed");
        }
    }

    @JavascriptInterface
    public void quit() {
        try {
            this.activity.runOnUiThread(new Runnable(){
                @Override
                public void run() {
                    quitWeb();
                }
            });
        } catch (Exception e) {

            Log.e("TAG", "quit: ", e);
        }
    }
}
